package com.nchuoj.judger.config;

import com.feign.config.enums.ExchangeNameEnum;
import com.feign.config.enums.FailureTime;
import com.feign.config.enums.QueueNameEnum;
import com.feign.config.enums.RouteKeyEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;

/**
 * TODO
 * @Description
 * @Author ZW
 * @Date 2022/12/21 11:13
 **/
@Configuration
@Slf4j
public class JudgeMq {

    @Value("${file.uploadFolder}")
    private void  createTestCaseFileDiv(String testCaseFileDiv){
        File file = new File(testCaseFileDiv);
        if (!file.exists()){
            log.info("创建目录 : {}",testCaseFileDiv);
            file.mkdirs();
        }
    }


    /**
     * 创建判题队列
     *
     * @return 注入的队列对象
     */
    @Bean("judgeQueue")
    public Queue createJudgeQueue() {
        log.info("创建判题队列");
        return new Queue(QueueNameEnum.JUDGE_QUEUE.getName(), true, false, false);
    }

    /**
     * 创建 qa 的消息队列
     */
    @Bean("qaQueue")
    public Queue createQaQueue() {
        log.info("创建QA队列");
        return new Queue(QueueNameEnum.QA_QUEUE.getName(), true, false, false);
    }

    /**
     * 创建 judge -> question服务的队列
     */
    @Bean("judge2QuestionQueue")
    public Queue createJudge2QuestionQueue() {
        log.info("创建judge->question队列");
        return new Queue(QueueNameEnum.JUDGE_2_QUESTION_QUEUE.getName(), true, false, false);
    }

    /**
     * 创建题目服务交换机
     */
    @Bean("questionDirectExchange")
    public DirectExchange createQuestionDirectExchange(){
        log.info("创建题目路由");
        return new DirectExchange(ExchangeNameEnum.QUESTION_DIRECT_EXCHANGE.getName(),true,false);
    }

    @Bean
    public Binding bindingQuestionFanoutExchangeToJudgeQueue(Queue judgeQueue,DirectExchange questionDirectExchange){
        return BindingBuilder.bind(judgeQueue).to(questionDirectExchange).with(RouteKeyEnum.QUESTION_2_JUDGE_AND_QA);
    }
    @Bean
    public Binding bindingQuestionFanoutExchangeToQaQueue(Queue qaQueue,DirectExchange questionDirectExchange){
        return BindingBuilder.bind(qaQueue).to(questionDirectExchange).with(RouteKeyEnum.QUESTION_2_JUDGE_AND_QA);
    }
    @Bean
    public Binding bindingQuestionFanoutExchangeToJudge2QuestionQueue(Queue judge2QuestionQueue,DirectExchange questionDirectExchange){
        return BindingBuilder.bind(judge2QuestionQueue).to(questionDirectExchange).with(RouteKeyEnum.JUDGE_2_QUESTION);
    }

    /**
     * 死信消息中转交换机 -- 用于中转过期的题目消息
     * @return
     */
    @Bean("expirationTimeFanoutExchange")
    public FanoutExchange createExpirationTimeFanoutExchange(){
        log.info("创建过期题目路由");
        return new FanoutExchange(ExchangeNameEnum.EXPIRATION_TIME_FANOUT_EXCHANGE.getName(),true,false);
    }
    /**
     * 预过期的题目消息 的交换机
     *
     */
    @Bean("createTimeFanoutExchange")
    public FanoutExchange createCreateTimeFanoutExchange(){
        log.info("创建 过期创建题目路由");
        return new FanoutExchange(ExchangeNameEnum.CREATE_TIME_FANOUT_EXCHANGE.getName(),true,false);
    }
    @Bean("expirationTimeQueue")
    public Queue createExpirationTimeQueue(){
        return QueueBuilder.durable(QueueNameEnum.EXPIRATION_TIME_QUEUE.getName()).ttl(FailureTime.WEEK.getTime()).deadLetterExchange(ExchangeNameEnum.EXPIRATION_TIME_FANOUT_EXCHANGE.getName()).build();
    }
    @Bean("estimateExpirationTimeQueue")
    public Queue createEstimateExpirationTimeQueue(){
        return  new Queue(QueueNameEnum.ESTIMATE_EXPIRATION_TIME_QUEUE.getName());
    }
    @Bean
    public Binding createEstimateExpiration2Queue(Queue expirationTimeQueue,FanoutExchange createTimeFanoutExchange){
        return  BindingBuilder.bind(expirationTimeQueue).to(createTimeFanoutExchange);
    }
}
